package com.ozo.boot.common.base.model;

import com.ozo.boot.common.base.exception.AbstractExceptionEnum;
import com.ozo.boot.common.base.exception.CommonExceptionEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

/**
 * 统一结果封装
 *
 * @author qiangesoft
 * @date 2024-06-28
 */
@Schema(description = "统一结果封装")
@Data
public class ResultVO<T> {

    @Schema(description = "状态码")
    private int code;

    @Schema(description = "提示信息")
    private String msg;

    @Schema(description = "数据")
    private T data;

    protected ResultVO() {
    }

    protected ResultVO(int code, String message, T data) {
        this.code = code;
        this.msg = message;
        this.data = data;
    }

    public static <T> ResultVO<T> success() {
        return success(null, CommonExceptionEnum.SUCCESS.getMessage());
    }

    public static <T> ResultVO<T> success(T data) {
        return success(data, CommonExceptionEnum.SUCCESS.getMessage());
    }

    public static <T> ResultVO<T> success(T data, String msg) {
        return new ResultVO(CommonExceptionEnum.SUCCESS.getCode(), msg, data);
    }

    public static <T> ResultVO<T> fail() {
        return fail(CommonExceptionEnum.INTERNAL_SERVER_ERROR);
    }

    public static <T> ResultVO<T> fail(String message) {
        return fail(CommonExceptionEnum.INTERNAL_SERVER_ERROR, message);
    }

    public static <T> ResultVO<T> fail(AbstractExceptionEnum errorCode) {
        return fail(errorCode.getCode(), errorCode.getMessage());
    }

    public static <T> ResultVO<T> fail(AbstractExceptionEnum errorCode, String message) {
        return fail(errorCode.getCode(), message);
    }

    public static <T> ResultVO<T> fail(int code, String message) {
        return new ResultVO(code, message, null);
    }

    public boolean isSuccess() {
        return this.code == CommonExceptionEnum.SUCCESS.getCode();
    }
}

